<template>
  <div>
    <el-card>
      <el-row :gutter="8">
        <el-col :span="8">
          <el-input
            placeholder="请输入搜索内容"
            v-model="queryInfo.query"
            clearable
            @clear="getUserList"
          />
        </el-col>
        <el-col :span="10">
          <el-button icon="el-icon-search" @click="getUserList">提交</el-button>
        </el-col>
        <el-col :span="6">
          <el-button type="primary" @click="addDialogVisible = true"
            >添加用户</el-button
          >
        </el-col>
      </el-row>
      <!--stripe隔行变色-->
      <el-table :data="userList" border stripe>
        <el-table-column type="index"></el-table-column>
        <el-table-column label="邮箱" prop="Email" />
        <el-table-column label="权限管理">
          <template #default="{ row }">
            <el-select @change="handleChange(row)" v-model="row.PermissionId">
              <el-option
                v-for="item in PerList"
                :key="item.Id"
                :label="item.PermissionName"
                :value="item.Id"
              >
              </el-option>
            </el-select>
          </template>
        </el-table-column>
        <el-table-column label="创建时间" prop="Psd" />
        <el-table-column v-slot="scope" label="操作">
          <el-button
            type="primary"
            icon="el-icon-edit"
            size="mini"
            @click="showEditDialog(scope.row.Id, scope.row.Email)"
          ></el-button>
          <el-button
            type="danger"
            icon="el-icon-delete"
            size="mini"
            @click="deleteUser(scope.row.Id)"
          ></el-button>
        </el-table-column>
      </el-table>
    </el-card>
    <div class="block">
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryInfo.pageNum"
        :page-sizes="[1, 3, 5]"
        :page-size="queryInfo.pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
    </div>
    <el-dialog
      title="添加用户"
      v-model="addDialogVisible"
      width="50%"
      @close="addDialogClosed"
    >
      <el-form
        :model="addForm"
        :rules="addFormRules"
        ref="addFormRef"
        label-width="70px"
      >
        <el-form-item label="邮箱" prop="Email">
          <el-input v-model="addForm.Email"></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="Psd">
          <el-input type="password" v-model="addForm.Psd"></el-input>
        </el-form-item>
      </el-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="addDialogVisible = false">取 消</el-button>
          <el-button type="primary" @click="addUser">确 定</el-button>
        </span>
      </template>
    </el-dialog>

    <el-dialog
      title="修改用户"
      v-model="editDialogVisible"
      width="50%"
      @close="editDialogClosed"
    >
      <el-form
        :model="editForm"
        :rules="editFormRules"
        ref="editFormRef"
        label-width="70px"
      >
        <el-form-item label="邮箱" prop="Email">
          <el-input v-model="editForm.Email" disabled></el-input>
        </el-form-item>
        <el-form-item label="密码">
          <el-input type="password" v-model="editPsd.OriginalPsd"></el-input>
        </el-form-item>
        <el-form-item label="新密码">
          <el-input type="password" v-model="editPsd.NewPsd"></el-input>
        </el-form-item>
      </el-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="editDialogVisible = false">取 消</el-button>
          <el-button type="primary" @click="editUserInfo">确 定</el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>
<script>
export default {
  created() {
    this.getUserList();
  },
  data() {
    return {
      queryInfo: {
        query: "",
        pageSize: 1,
        pageNum: 5,
      },
      userList: [],
      PerList: [],
      total: 0,
      addDialogVisible: false,
      addForm: {
        Email: "",
        Psd: "",
      },
      total: null,
      editForm: {
        Id: null,
        Email: null,
      },
      editPsd: {},
      editDialogVisible: false,
      addFormRules: {
        Email: [
          { required: true, message: "请输入邮箱", trigger: "blur" },
          { type: "email", message: "请输入正确邮箱地址", trigger: "blur" },
        ],
        Psd: [
          { required: true, message: "请输入密码", trigger: "blur" },
          { min: 3, max: 8, message: "长度在 3 到 8 个字符", trigger: "blur" },
        ],
      },
      editFormRules: {
        Email: [
          { message: "请输入邮箱", trigger: "blur" },
          { type: "email", message: "请输入正确邮箱地址", trigger: "blur" },
        ],
        Psd: [
          { required: true, message: "请输入密码", trigger: "blur" },
          { min: 3, max: 8, message: "长度在 3 到 8 个字符", trigger: "blur" },
        ],
      },
    };
  },
  methods: {
    handleSizeChange(newSize) {
      this.queryInfo.pageSize = newSize;
      this.getUserList();
    },
    handleCurrentChange(newPage) {
      this.queryInfo.pageNum = newPage;
      this.getUserList();
    },
    async handleChange(row) {
      console.log(row.Id);
      console.log(row.PermissionId);
      await this.$api.user
        .editPermission(Id, PermissionId)
        .then((data) => {
          if ((data.flag = "success")) this.$message.success("权限修改成功");
        });
    },
    handleCommand(command) {
      this.$message("click on item " + command);
    },
    async getUserList() {
      await this.$api.user.getUserList(this.queryInfo).then((data) => {
        console.log(data, 1111);
        this.userList = data.data;
        this.total = data.total;
      });
      await this.$api.user.getPermissions().then((data) => {
        this.PerList = data.data;
      });
    },
    addDialogClosed() {
      this.$refs.addFormRef.resetFields();
    },
    addUser() {
      this.$refs.addFormRef.validate(async (valid) => {
        if (!valid) return;
        await this.$api.user.addUser(this.addForm).then((data) => {
          if (data.flag != "success") return this.$message.error(data.msg);
          this.$message.success("操作成功");
          this.addDialogVisible = false;
          this.getUserList();
        });
      });
    },
    async deleteUser(id) {
      const confirmResult = await this.$confirm("确定删除", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      }).catch((err) => err);
      if (confirmResult != "confirm") {
        return this.$message.info("取消删除");
      }
      await this.$api.user.deleteUser(id).then((data) => {
        if (data.code != 0) {
          return this.$message.error("删除失败");
        }
        this.$message.success("删除成功");
        this.getUserList();
      });
    },
    showEditDialog(id, email) {
      this.editDialogVisible = true;
      (this.editForm.Id = id), (this.editForm.Email = email);
    },
    editDialogClosed() {
      this.$refs.editFormRef.resetFields();
    },
    editUserInfo() {
      if (this.editPsd.OriginalPsd == this.editPsd.NewPsd) {
        this.$message.warning("原密码与新密码一致");
        return;
      }
      this.$refs.editFormRef.validate((valid) => {
        if (!valid) return;
        this.$api.user
          .editUser({
            Id: this.editForm.Id,
            OriginalPsd: this.editPsd.OriginalPsd,
            NewPsd: this.editPsd.NewPsd,
          })
          .then((data) => {
            if (data.flag != "success") return this.$message.error(data.msg);
            this.$message.success("操作成功");
            this.editPsd.OriginalPsd = null;
            this.editPsd.NewPsd = null;
            this.editDialogVisible = false;
            this.getUserList();
          });
      });
    },
  },
};
</script>
<style scoped>
.el-dropdown-link {
  cursor: pointer;
  color: #409eff;
}
.el-icon-arrow-down {
  font-size: 12px;
}
</style>